<?php
@header("Content-type:text/html;charset=GB2312");
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcomee extends CI_Controller
{

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *        http://example.com/index.php/welcome
     *    - or -
     *        http://example.com/index.php/welcome/index
     *    - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $db1 = $this->load->database('default', TRUE);
        $db2 = $this->load->database('sqlserver', TRUE);
        /*        $result1 = $db1->query('select * from admin')->result_array();
                foreach ($result1 as $row1) {
                    var_dump($row1['name']);
                }*/

        /*将SQLSERVER数据转换为MYSQL数据*/
        /*
                $adminsql = $db2->query('select * from NC_Admin')->result_array();
                foreach ($adminsql as $i => $row2) {
                    $admin[$i]['id'] = $row2['id'];
                    $admin[$i]['username'] = $row2['username'];
                    $admin[$i]['password'] = $row2['password'];
                    $admin[$i]['status'] = $row2['status'];
                    $admin[$i]['grade'] = $row2['AdminGrade'];
                    $admin[$i]['flag'] = $row2['Adminflag'];
                    $admin[$i]['time'] = $row2['LoginTime'];
                    $admin[$i]['ip'] = $row2['Loginip'];
                    $admin[$i]['islock'] = $row2['isLock'];
                    $admin[$i]['code'] = $row2['RandomCode'];
                    $admin[$i]['islogin'] = $row2['isAloneLogin'];
                }
                //$this->db->insert('admin', $cont);
                $this->db->insert_batch('admin', $admin);

                $tagsql = $db2->query('select * from fy_TAGS')->result_array();
                foreach ($tagsql as $i => $row3) {
                    $tag[$i]['id'] = $row3['Id'];
                    $tag[$i]['name'] = $row3['TagName'];
                    $tag[$i]['softid'] = $row3['SoftId'];
                    $tag[$i]['numbers'] = $row3['TagNumbers'];
                }
                $this->db->insert_batch('tag', $tag);

                $articlesql = $db2->query('select * from NC_Article')->result_array();
                foreach ($articlesql as $i => $row4) {
                    $article[$i]['ArticleID'] = $row4['ArticleID'];
                    $article[$i]['ChannelID'] = $row4['ChannelID'];
                    $article[$i]['ClassID'] = $row4['ClassID'];
                    $article[$i]['SpecialID'] = $row4['SpecialID'];
                    $article[$i]['title'] = $row4['title'];
                    $article[$i]['subtitle'] = $row4['subtitle'];
                    $article[$i]['ColorMode'] = $row4['ColorMode'];
                    $article[$i]['FontMode'] = $row4['FontMode'];
                    $article[$i]['BriefTopic'] = $row4['BriefTopic'];
                    $article[$i]['content'] = $row4['content'];
                    $article[$i]['OuterLinks'] = $row4['OuterLinks'];
                    $article[$i]['Related'] = $row4['Related'];
                    $article[$i]['Author'] = $row4['Author'];
                    $article[$i]['ComeFrom'] = $row4['ComeFrom'];
                    $article[$i]['isTop'] = $row4['isTop'];
                    $article[$i]['AllHits'] = $row4['AllHits'];
                    $article[$i]['DayHits'] = $row4['DayHits'];
                    $article[$i]['WeekHits'] = $row4['WeekHits'];
                    $article[$i]['MonthHits'] = $row4['MonthHits'];
                    $article[$i]['HitsTime'] = $row4['HitsTime'];
                    $article[$i]['WriteTime'] = $row4['WriteTime'];
                    $article[$i]['HtmlFileDate'] = $row4['HtmlFileDate'];
                    $article[$i]['username'] = $row4['username'];
                    $article[$i]['star'] = $row4['star'];
                    $article[$i]['isBest'] = $row4['isBest'];
                    $article[$i]['ImageUrl'] = $row4['ImageUrl'];
                    $article[$i]['UploadImage'] = $row4['UploadImage'];
                    $article[$i]['UserGroup'] = $row4['UserGroup'];
                    $article[$i]['PointNum'] = $row4['PointNum'];
                    $article[$i]['isUpdate'] = $row4['isUpdate'];
                    $article[$i]['isAccept'] = $row4['isAccept'];
                    $article[$i]['AlphaIndex'] = $row4['AlphaIndex'];
                    $article[$i]['AutoPages'] = $row4['AutoPages'];
                    $article[$i]['isxmltext'] = $row4['isxmltext'];
                    $article[$i]['xmlfilename'] = $row4['xmlfilename'];
                }
                $this->db->insert_batch('article', $article);

                $channelsql = $db2->query('select * from NC_Channel')->result_array();
                foreach ($channelsql as $i => $row5) {
                    $channel[$i]['ChannelID'] = $row5['ChannelID'];
                    $channel[$i]['orders'] = $row5['orders'];
                    $channel[$i]['ColorModes'] = $row5['ColorModes'];
                    $channel[$i]['FontModes'] = $row5['FontModes'];
                    $channel[$i]['ChannelName'] = $row5['ChannelName'];
                    $channel[$i]['Caption'] = $row5['Caption'];
                    $channel[$i]['ChannelDir'] = $row5['ChannelDir'];
                    $channel[$i]['StopChannel'] = $row5['StopChannel'];
                    $channel[$i]['IsHidden'] = $row5['IsHidden'];
                    $channel[$i]['LinkTarget'] = $row5['LinkTarget'];
                    $channel[$i]['ChannelType'] = $row5['ChannelType'];
                    $channel[$i]['ChannelUrl'] = $row5['ChannelUrl'];
                    $channel[$i]['modules'] = $row5['modules'];
                    $channel[$i]['ModuleName'] = $row5['ModuleName'];
                    $channel[$i]['BindDomain'] = $row5['BindDomain'];
                    $channel[$i]['DomainName'] = $row5['DomainName'];
                    $channel[$i]['ChannelSkin'] = $row5['ChannelSkin'];
                    $channel[$i]['IsCreateHtml'] = $row5['IsCreateHtml'];
                    $channel[$i]['HtmlExtName'] = $row5['HtmlExtName'];
                    $channel[$i]['StopUpload'] = $row5['StopUpload'];
                    $channel[$i]['MaxFileSize'] = $row5['MaxFileSize'];
                    $channel[$i]['UpFileType'] = $row5['UpFileType'];
                    $channel[$i]['IsAuditing'] = $row5['IsAuditing'];
                    $channel[$i]['AppearGrade'] = $row5['AppearGrade'];
                    $channel[$i]['PostGrade'] = $row5['PostGrade'];
                    $channel[$i]['LeastString'] = $row5['LeastString'];
                    $channel[$i]['MaxString'] = $row5['MaxString'];
                    $channel[$i]['PaginalNum'] = $row5['PaginalNum'];
                    $channel[$i]['LeastHotHist'] = $row5['LeastHotHist'];
                    $channel[$i]['Channel_Setting'] = $row5['Channel_Setting'];
                    $channel[$i]['SortDestination'] = $row5['SortDestination'];
                    $channel[$i]['InfoDestination'] = $row5['InfoDestination'];
                    $channel[$i]['MoreDestination'] = $row5['MoreDestination'];
                    $channel[$i]['setEditor'] = $row5['setEditor'];
                    $channel[$i]['NamedPath'] = $row5['NamedPath'];
                }
                $this->db->insert_batch('channel', $channel);

                $classifysql = $db2->query('select * from NC_Classify')->result_array();
                foreach ($classifysql as $i => $row6) {
                    $classify[$i]['ClassID'] = $row6['ClassID'];
                    $classify[$i]['ChannelID'] = $row6['ChannelID'];
                    $classify[$i]['rootid'] = $row6['rootid'];
                    $classify[$i]['depth'] = $row6['depth'];
                    $classify[$i]['ClassName'] = $row6['ClassName'];
                    $classify[$i]['ColorModes'] = $row6['ColorModes'];
                    $classify[$i]['FontModes'] = $row6['FontModes'];
                    $classify[$i]['Readme'] = $row6['Readme'];
                    $classify[$i]['orders'] = $row6['orders'];
                    $classify[$i]['parentid'] = $row6['parentid'];
                    $classify[$i]['ParentStr'] = $row6['ParentStr'];
                    $classify[$i]['child'] = $row6['child'];
                    $classify[$i]['ChildStr'] = $row6['ChildStr'];
                    $classify[$i]['LinkTarget'] = $row6['LinkTarget'];
                    $classify[$i]['TurnLink'] = $row6['TurnLink'];
                    $classify[$i]['TurnLinkUrl'] = $row6['TurnLinkUrl'];
                    $classify[$i]['UserGroup'] = $row6['UserGroup'];
                    $classify[$i]['ClassDir'] = $row6['ClassDir'];
                    $classify[$i]['HtmlFileDir'] = $row6['HtmlFileDir'];
                    $classify[$i]['UseHtml'] = $row6['UseHtml'];
                    $classify[$i]['skinid'] = $row6['skinid'];
                    $classify[$i]['ShowCount'] = $row6['ShowCount'];
                    $classify[$i]['isUpdate'] = $row6['isUpdate'];
                    $classify[$i]['AdsCode'] = $row6['AdsCode'];
                    $classify[$i]['stopad'] = $row6['stopad'];
                    $classify[$i]['phoneimg'] = $row6['phoneimg'];
                }
                $this->db->insert_batch('classify', $classify);
        *//*
        $downaddresssql = $db2->query('select * from NC_DownAddress')->result_array();
        foreach ($downaddresssql as $i => $row7) {
            $downaddress[$i]['id'] = $row7['id'];
            $downaddress[$i]['ChannelID'] = $row7['ChannelID'];
            $downaddress[$i]['softid'] = '' .$row7['softid']. '';
            $downaddress[$i]['downid'] = $row7['downid'];
            $downaddress[$i]['DownFileName'] = $row7['DownFileName'];
            $downaddress[$i]['DownText'] = $row7['DownText'];
        }
        $this->db->insert_batch('downaddress', $downaddress);

                $downserversql = $db2->query('select * from NC_DownServer')->result_array();
                foreach ($downserversql as $i => $row8) {
                    $downserver[$i]['id'] = $row8['DownID'];
                    $downserver[$i]['sitename'] = $row8['DownloadName'];
                    $downserver[$i]['siteurl'] = $row8['DownloadPath'];
                    $downserver[$i]['listorder'] = '';
                    $downserver[$i]['siteid'] = '';
                }
                $this->db->insert_batch('phpcms_downservers', $downserver , 'id');
                var_dump("<pre>");
                var_dump($downserver);
*/

        $size = 500;
        $page = isset($_GET['page']) ? $_GET['page'] : 1;
        $zongshu = $page * $size;

        $sql = "select top $size * from (select top $zongshu  * from NC_SoftList order by SoftID asc) NC_SoftList order by SoftID desc";
        $downloadsql = $db2->query($sql)->result_array();
        if ($downloadsql) {
            //$downloadsql = $db2->query('select top 200 * from NC_SoftList where not exists (select top 20170 * from NC_SoftList)')->result_array();
            $download = array();
            foreach ($downloadsql as $row9) {
                //if ($db1->query('select * from phpcms_download where id = '.$row9['SoftID'])->result_array()) {
                    $newitem = array(
                        'id' => $row9['SoftID'],
                        'catid' => $row9['ChannelID'],
                        'typeid' => '',
                        'title' => $row9['SoftName'],
                        'style' => '',
                        'thumb' => $row9['SoftImage'],
                        'keywords' => $row9['TAGS'],
                        'description' => $row9['Content'],
                        'posids' => '1',
                        'url' => 'http://127.0.0.1/index.php?m=content&c=index&a=show&catid=' . $row9['ChannelID'] . '&id=' . $row9['SoftID'],
                        'listorder' => '0',
                        'status' => '99',
                        'sysadd' => '1',
                        'islink' => '0',
                        'username' => $row9['username'],
                        'inputtime' => $row9['SoftTime'],
                        'updatetime' => $row9['HitsTime'],
                        'systems' => $row9['RunSystem'],
                        'copytype' => $row9['impower'],
                        'language' => $row9['Languages'],
                        'classtype' => $row9['SoftType'],
                        'filesize' => $row9['Softsize'],
                        'stars' => $row9['star'],
                        'subtitle' => $row9['subtitle'],
                        'up' => $row9['AllHits'],
                        'down' => $row9['AllHits'],
                    );
                    $download[] = $newitem;
                //}
            }
            $this->db->insert_batch('phpcms_download', $download, 'id');
            var_dump("<pre>");
            var_dump($download);
        }
        $next = $page + 1;
        $ctrs = "<script>
function update(){
    self.location.href='http://localhost/index.php?page=$next';
}
setTimeout('update()',3000);
</script>";
        echo($ctrs);
        /*$conn1 = odbc_connect("jz5u", "sa", "wrmake519201!@#");
        $sql1 = "select * from NC_Comment";
        $exec = odbc_exec($conn1, $sql1);//执行语句

        while ($row = odbc_fetch_array($exec)) {
            //var_dump($row['CommentID']);
            //var_dump($row['ChannelID']);
            //var_dump($row['postid']);
            //var_dump($row['content']);
        }
        $conn2 = mysql_connect("localhost", "root", "chuanmai");
        mysql_select_db("jz5u", $con);
        $sql2 = "select * from admin";
        $res = mysql_query($sql2);
        while ($row = mysql_fetch_array($res)) {
            var_dump($row['id']);
        }
        mysql_close($conn2);

        $sql = "SELECT * FROM NC_Comment";
        $query = $this->db->query($sql);
        $result = $query->result_array();
        foreach ($result as $value) {
            var_dump($value);
        }
        exit;*/
    }
}